<?php

namespace app\command;

use app\common\model\Clients;
use app\common\model\MachineWithClient;
use app\common\model\OfficialUsers;
use app\common\model\Tasks as M;
use think\console\input\Argument;
use think\console\Command;
use think\console\Input;
use think\console\Output;


class Ztasks extends Command
{

    private $task_flag =  './common/state/task';
    private $update_flag =  './common/state/update';
    private $sleep = 2;

    protected function configure()
    {
        //        parent::configure();
        $this->setName('ztasks')
            ->addArgument('method', Argument::OPTIONAL, "方法")
            ->setDescription('通知任务 任务命令');
    }


    protected function execute(Input $input, Output $output)
    {
        $method = $input->getArgument('method');

        switch ($method) {
            case 'start':
                $output->writeln($this->start());
                break;
            case 'stop':
                $output->writeln($this->stop());
                break;
            case 'restart':
                $output->writeln($this->stop());
                sleep(1);
                $output->writeln($this->start());
                break;
            default:
                $output->writeln(<<<COMM
参数:
    start: 开始通知任务
    stop: 结束通知任务
    restart: 重启任务
COMM
                );
        }
    }


    private function getKeyV($key)
    {
        switch ($key) {
            case 'task';
                return $this->task_flag;
                break;
            case 'update':
                return $this->update_flag;
                break;
        }
        return null;
    }


    private function getState($key)
    {
        return @file_get_contents($this->getKeyV($key));
    }


    private function setState($key, $state)
    {
        return file_put_contents($this->getKeyV($key), $state);
    }


    /**
     * 启动
     */
    protected function start()
    {
        $is_run = $this->getState('task');
        if ($is_run != 'yes') {
            $this->setState('task', 'yes');
            $task = $this->getState('task');
            if ($task == 'yes') {
                $this->R();
                return 'ztasks has run!';
            }
            return 'start failed!';
        }
        return 'ztasks has already start!';
    }


    /**
     * 关闭
     */
    protected function stop()
    {
        $this->setState('task', 'no');
        $task = $this->getState('task');
        if ($task == 'no')
            return 'ztasks has stop!';
        return 'stop failed!';
    }


    /**
     * 运行
     */
    protected function R()
    {
        $m         = new M();
        $task_data = $m->getAll();

        ignore_user_abort(true);
        set_time_limit(0);

        while (1) {

            $task = $this->getState('task');
            if ($task == 'no') {
                break;
            }

            $update = $this->getState('update');
            print($update);

            if (trim($update) == 'yes') {//更新任务后
//                print($update);
                $task_data = $m->getAll();
                $this->setState('update', 'no');
            }

            foreach ($task_data as $val) {
                $this->SM($val, $val['machine_type'] . ' is send!');
            }

            sleep($this->sleep);
        }
    }


    /**
     * 发送信息
     */
    protected function SM($val, $msg)
    {
        // TODO: 消息代码

        /*小程序openid
        */
        $mwc      = new MachineWithClient();
        $rels     = $mwc->getByType($val['machine_type']);
        $unionids = array_map(function ($val) {
            return $val['client']['unionid'];
        }, $rels);
        $ou       = new OfficialUsers();
        $openids  = [];
        foreach ($unionids as $val) {
            $tmp       = $ou->getOpenid();
            $openids[] = $tmp;
        }


        /*微信消息
        $app = app('wechat.official_account')
        $app->server->push(function($message){
            return 'hello 为';
        });
        $app->server->serve()->send();
        */


    }
}


